package cn.edu.imust.loginregisterdemo.home;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.uuzuche.lib_zxing.BuildConfig;

import java.util.List;

import cn.edu.imust.loginregisterdemo.R;
import cn.edu.imust.loginregisterdemo.home.model.domain.DownDataResult;
import cn.edu.imust.loginregisterdemo.utils.ToBase64Util;
import cn.edu.imust.loginregisterdemo.utils.Utils;


public abstract class RecyclerViewBaseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    protected final List<DownDataResult.DataBean> mData;
    private OnItemClickListener    mOnItemClickListener;

    public RecyclerViewBaseAdapter(List<DownDataResult.DataBean> data) {
        this.mData = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = getSubView(parent, viewType);
        return new InnerHolder(view);
    }

    protected abstract View getSubView(ViewGroup parent, int viewType);

    /**
     * 这个方法是用于绑定holder的,一般用来设置数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //在这里设置数据
        ((InnerHolder) holder).setData(mData.get(position), position);
    }


    /**
     * 反回条目的个数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        //设置一个监听,其实,就是要设置一个接口,一个回调的接口
        this.mOnItemClickListener = listener;
    }

    /**
     * 编写回调的步骤
     * 1、创建这个接口
     * 2、定义借口内部的方法
     * 3、提供设置接口的方法(其实是外部实现)
     * 4、接口方法的调用
     */
    public interface OnItemClickListener {
        void onItemClick(int position);
        boolean onItemPictureClick(ImageView imageView, int position, DownDataResult.DataBean dataBean);
        boolean onItemDetailClick(TextView textView, int position, DownDataResult.DataBean dataBean);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {


        private TextView mTitle;
        private TextView mTime;
        private TextView mDescription;
        private ImageView mPicture;
        private TextView mLocation;
        private TextView mDetail;
        private int mPosition;


        public InnerHolder(View itemView) {
            super(itemView);

            //找到条目的控件
            mTitle = itemView.findViewById(R.id.title);
            mTime = itemView.findViewById(R.id.time);
            mDescription = itemView.findViewById(R.id.description);
            mPicture = itemView.findViewById(R.id.picture);
            mLocation = itemView.findViewById(R.id.location);
            mDetail = itemView.findViewById(R.id.detail);

            mDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemDetailClick(mDetail, mPosition, mData.get(mPosition));
                    }
                }
            });
            mPicture.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemPictureClick(mPicture, mPosition, mData.get(mPosition));
                    }
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(mPosition);
                    }
                }
            });
        }

        /**
         * 这个方法用于设置数据
         */
        public void setData(DownDataResult.DataBean dataBean, int position) {

            this.mPosition = position;
            //开始设置数据
            if (BuildConfig.DEBUG) Log.d("InnerHolder", "mData position --> " + position);
            mTitle.setText(dataBean.getUsername());
            mTime.setText(dataBean.getTime());
            mDescription.setText(dataBean.getEdit());
            String gps = dataBean.getGps();
            String latitude = Utils.locationStringFormat(gps, 10, 19);
            String longitude = Utils.locationStringFormat(gps, 24, gps.length()-1);
            if (BuildConfig.DEBUG) Log.d("InnerHolder", "username-->" + dataBean.getUsername() +", "
             + "time-->" + dataBean.getTime() + ", " + "gps-->" + gps);
            if (dataBean.getPic() != null) {
                mPicture.setImageBitmap(ToBase64Util.Base64ToBitMap(dataBean.getPic()));
            }
            mLocation.setText("定位信息:\n" + "纬度:" + latitude + ", " + "经度:" + longitude);
        }
    }
}
